﻿package classes 
{
	import alternativa.engine3d.core.Face;
	import alternativa.engine3d.materials.TextureMaterial;
	import alternativa.engine3d.primitives.Box;
	import alternativa.types.Texture;
	import alternativa.engine3d.materials.MovieClipMaterial;
	
	import flash.geom.Point;
	import flash.display.BitmapData;
	
	import classes.loadExternalSWF;
		
	public class skyBox extends Box 
	{
		private var externalFront:loadExternalSWF = new loadExternalSWF("images/sky/front.jpg");
		private var materialFront:MovieClipMaterial = new MovieClipMaterial(externalFront, 512, 512);
		private var externalBack:loadExternalSWF = new loadExternalSWF("images/sky/back.jpg");
		private var materialBack:MovieClipMaterial = new MovieClipMaterial(externalBack, 512, 512);
		private var externalLeft:loadExternalSWF = new loadExternalSWF("images/sky/left.jpg");
		private var materialLeft:MovieClipMaterial = new MovieClipMaterial(externalLeft, 512, 512);
		private var externalRight:loadExternalSWF = new loadExternalSWF("images/sky/right.jpg");
		private var materialRight:MovieClipMaterial = new MovieClipMaterial(externalRight, 512, 512);
		private var externalTop:loadExternalSWF = new loadExternalSWF("images/sky/top.jpg");
		private var materialTop:MovieClipMaterial = new MovieClipMaterial(externalTop, 512, 512);
		private var externalBottom:loadExternalSWF = new loadExternalSWF("images/sky/bottom.jpg");
		private var materialBottom:MovieClipMaterial = new MovieClipMaterial(externalBottom, 512, 512);
		
		private static const rotateTop:Number = 0;
		private static const rotateBottom:Number = 180;
			
		public function skyBox():void
		{
				
			// large box with inward face normales
			super (50000, 50000, 50000, 1, 1, 1, true);
			
			// assign materials
			setMaterialToSurface (materialFront, "front"); 
			materialFront.repeat = false; 
			materialFront.smooth = true;
			setMaterialToSurface (materialBack, "back"); 
			materialBack.repeat = false; 
			materialBack.smooth = true;
			setMaterialToSurface (materialLeft, "left"); 
			materialLeft.repeat = false; 
			materialLeft.smooth = true;
			setMaterialToSurface (materialRight, "right"); 
			materialRight.repeat = false; 
			materialRight.smooth = true;
			setMaterialToSurface (materialTop, "top"); 
			materialTop.repeat = false; 
			materialTop.smooth = true;
			setMaterialToSurface (materialBottom, "bottom"); 
			materialBottom.repeat = false; 
			materialBottom.smooth = true;
			
			var i:int, n:int, f:Face;
			
			// rotate top
			f = surfaces ["top"].faces.peek ();
			n = Math.max (0, rotateTop / 90);
			for (i = 0; i < n; i++) rotateFaceBy90 (f);
			
			// rotate bottom
			f = surfaces ["bottom"].faces.peek ();
			n = Math.max (0, rotateBottom / 90);
			for (i = 0; i < n; i++) rotateFaceBy90 (f);
		}
		
		private function rotateFaceBy90 (f:Face):void 
		{
			var aUV:Point, bUV:Point, cUV:Point;
			aUV = f.aUV;
			bUV = f.bUV;
			cUV = f.cUV;
			f.aUV = aUV.add (cUV).subtract (bUV);
			f.bUV = aUV;
			f.cUV = bUV;
		}
		
	}
}